package com.example.demo.controller;

import com.example.demo.service.SystemInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
public class SystemInfoController {

    private final SystemInfoService systemInfoService;

    @Autowired
    public SystemInfoController(SystemInfoService systemInfoService) {
        this.systemInfoService = systemInfoService;
    }

    // 主页面
    @GetMapping("/")
    public String index(Model model) {
        // 初始加载一些基本信息
        model.addAttribute("hostInfo", systemInfoService.getHostInfo());
        model.addAttribute("cpuInfo", systemInfoService.getCpuInfo());
        model.addAttribute("memoryInfo", systemInfoService.getMemoryInfo());
        return "index";
    }

    // 获取主机信息的API
    @GetMapping("/api/host")
    @ResponseBody
    public Map<String, Object> getHostInfo() {
        return systemInfoService.getHostInfo();
    }

    // 获取CPU信息的API
    @GetMapping("/api/cpu")
    @ResponseBody
    public Map<String, Object> getCpuInfo() {
        return systemInfoService.getCpuInfo();
    }

    // 获取内存信息的API
    @GetMapping("/api/memory")
    @ResponseBody
    public Map<String, Object> getMemoryInfo() {
        return systemInfoService.getMemoryInfo();
    }

    // 获取磁盘信息的API
    @GetMapping("/api/disks")
    @ResponseBody
    public Object getDiskInfo() {
        return systemInfoService.getDiskInfo();
    }

    // 获取文件系统信息的API
    @GetMapping("/api/filesystems")
    @ResponseBody
    public Object getFileSystemInfo() {
        return systemInfoService.getFileSystemInfo();
    }

    // 获取网络信息的API
    @GetMapping("/api/networks")
    @ResponseBody
    public Object getNetworkInfo() {
        return systemInfoService.getNetworkInfo();
    }

    // 获取进程信息的API
    @GetMapping("/api/processes")
    @ResponseBody
    public Object getProcessInfo() {
        return systemInfoService.getProcessInfo();
    }

    // 获取所有系统信息的API
    @GetMapping("/api/all")
    @ResponseBody
    public Map<String, Object> getAllSystemInfo() {
        return systemInfoService.getAllSystemInfo();
    }
    
    // 获取系统负载信息的API
    @GetMapping("/api/load")
    @ResponseBody
    public Map<String, Object> getLoadInfo() {
        return systemInfoService.getLoadInfo();
    }
    
    // 获取系统运行时间信息的API
    @GetMapping("/api/uptime")
    @ResponseBody
    public Map<String, Object> getUptimeInfo() {
        return systemInfoService.getUptimeInfo();
    }
}